<?php

namespace app\selladmin\controller;

use app\selladmin\controller\CommonController;
use app\selladmin\model\CtmStaff;
use app\selladmin\model\Order;
use app\selladmin\model\Store;
use app\util\Utils;
use think\Db;

class OrderController extends CommonController {
    /*
     * 订单详情
     * author xfwed 
     * date 2017.08.04
     */

    public function OrderDetails() {
        $id = input('param.id');
        //查询订单信息
        $Order = new Order();
        $orderdetails = $Order->OrderDetails($id);
        //查询预约信息
        $ctmstaff = new CtmStaff();
        $onlinelist = $ctmstaff->OnlineInfo('', '', $id, 10);
        return view('orderdetails', [
            'orderdetails' => $orderdetails,
            'onlinelist' => $onlinelist,
        ]);
    }

    /*
     * 订单列表
     * author xfwed 
     * date 2017.08.08
     */

//
//    public function OrderList() {
//        //返回审核人员列表
//        $adminlist = Db::name('admin')->where('id!=1 and type=1')->field('id,admin_note')->select();
//
//        $id = input('param.id');
//        $admin = input('get.adminid');
//        //查询订单信息
//        $order = new Order();
//        $ord_num = input('get.ord_num');
//        $orderList = $order->OrderInfo('', $ord_num, '', 12);
//        for ($i = 0; $i < count($orderList); $i++) {
//            //查询店铺信息
//            $store = new Store();
//            $orderList[$i]['storeinfo'] = $store->StoreInfo($orderList[$i]['store_id']);
//        }
//
//        return view('', [
//            'orderList' => $orderList,
//            'ord_num' => $ord_num,
//            'id' => $id,
//            'adminlist' => $adminlist,
//            'admin' => $admin,
//        ]);
//    }

    public function OrderList() {
        //返回审核人员列表
        $adminlist = Db::name('admin')->where('id!=1 and type=1')->field('id,admin_note')->select();

        $id = input('param.id');
        $admin = input('param.adminid');
        $status = input('param.status');
        $ord_num = input('param.ord_num');
        $name=input('param.name');
        $phone=  input('param.phone');
        if($name){
            $where['s.store_name']=array('like','%'.$name.'%');
            $query['name'] = $name;
        }
        if($phone){
            $where['s.keeper_phone']=$phone;
            $query['phone'] = $phone;
        }
        if ($ord_num) {
            $where['o.ord_num'] = $ord_num;
            $query['ord_num'] = $ord_num;
        }
        if ($admin) {
            $where['s.adminid'] = $admin;
            $query['adminid'] = $admin;
        }
        if ($status) {
            $where['o.status'] = ['>', 1];
            $query['status'] = $status;
        }

        $starttime = input('param.starttime');
        $endtime = input('param.endtime');
        if ($endtime && $starttime) {
            $time = strtotime($starttime);
            $time2 = strtotime($endtime) + 24 * 60 * 60;
            $where['o.add_time'] = array('between', array($time, $time2));
            $query['starttime'] = $starttime;
            $query['endtime'] = $endtime;
        } elseif (empty($endtime) && $starttime) {
            $time = strtotime($starttime);
            $time2 = $time + 24 * 60 * 60;
            $where['o.add_time'] = array('between', array($time, $time2));
            $query['starttime'] = $starttime;
        }
        $where = isset($where) ? $where : [];
        $query = isset($query) ? $query : [];

        //查询订单信息
        $orderList = Db::table('da_order')
                ->alias('o')
                ->join('custom c', 'o.ctm_id = c.id')
                ->join('store s', 'o.store_id = s.id')
                ->where($where)
                ->order('add_time desc')
                ->field("o.id,o.source,o.ord_num,o.ctm_id,o.ordtype,o.pay_money,o.status,o.add_time,o.pay_time,o.store_id,c.custom_name,c.real_name,c.custom_phone,s.store_name")
                ->paginate(12)
                ->appends($query);
        //查询订单总金额
        $where['o.ordtype']=array('in','1,3,4,5,6,7');
        $where['o.store_id']=array('<>','162');
        $where['o.pay_money']=array('>',0);
        $where['o.status'] = ['>', 1];
        $orderMoney = Db::table('da_order')
                ->alias('o')
                ->join('custom c', 'o.ctm_id = c.id','left')
                ->join('store s', 'o.store_id = s.id','left')
                ->where($where)
                ->field("o.id,o.source,o.ord_num,o.ctm_id,o.ordtype,o.pay_money,o.status,o.add_time,o.pay_time,o.store_id,c.custom_name,c.real_name,c.custom_phone,s.store_name")
                ->sum('pay_money');

        return view('orderlist', [
            'orderList' => $orderList,
            'ord_num' => $ord_num,
            'id' => $id,
            'adminlist' => $adminlist,
            'admin' => $admin,
            'starttime' => $starttime,
            'endtime' => $endtime,
            'status' => $status,
            'name'=>$name,
            'phone'=>$phone,
            'orderMoney'=>$orderMoney
        ]);
    }

    /*
     * 订单分析
     * author xfwed
     * date 2018-01-16
     * */

    public function OrderEcharts() {
        $year = isset($_REQUEST['year']) ? $_REQUEST['year'] : date('Y');
        //月份
        for ($m = 1; $m <= 12; $m++) {
            $month[] = $m . "月";
        }

        //订单量（总）
        $orderData = Order::where("FROM_UNIXTIME(add_time,'%Y') = '" . $year . "'")
                ->field("FROM_UNIXTIME(add_time, '%m') AS m,count(id) AS c")
                ->group('m')
                ->order('m asc')
                ->select();
        $orderReturn_data = array(
            '01' => 0, '02' => 0, '03' => 0, '04' => 0, '05' => 0, '06' => 0, '07' => 0, '08' => 0, '09' => 0, '10' => 0, '11' => 0, '12' => 0,
        );
        foreach ($orderData as $value) {
            $orderReturn_data[$value['m']] = $value['c'];
        }

        //订单量（有效）
        $orderData1 = Order::where("FROM_UNIXTIME(add_time,'%Y') = '" . $year . "'")
                ->where('status', '=', 2)
                ->field("FROM_UNIXTIME(add_time, '%m') AS m,count(id) AS c")
                ->group('m')
                ->order('m asc')
                ->select();
        $orderReturn_data1 = array(
            '01' => 0, '02' => 0, '03' => 0, '04' => 0, '05' => 0, '06' => 0, '07' => 0, '08' => 0, '09' => 0, '10' => 0, '11' => 0, '12' => 0,
        );
        foreach ($orderData1 as $value) {
            $orderReturn_data1[$value['m']] = $value['c'];
        }

        $currentYear = date('Y');
        $years = $currentYear + 1;

        return view('orderecharts', [
            'month' => implode(',', $month),
            'orderData' => implode(',', array_values($orderReturn_data)), //订单量总
            'orderData1' => implode(',', array_values($orderReturn_data1)), //订单量有效
            'years' => $years,
            'year' => $year,
            'currentYear' => $currentYear
        ]);
    }

}
